/**Parser */
export const MAX_BLOCK_CHARS = 1000;
export const MIN_BLOCK_CHARS = 50;
export const MIN_CHUNK_REMAINDER_CHARS = 200; // Minimum characters for the *next* chunk after a split
export const MAX_CHARS_TOLERANCE_FACTOR = 1.15; // 15% tolerance for max chars

/**Overlap Strategy */
export const OVERLAP_CHARS = 200; // Characters of overlap between adjacent chunks
export const OVERLAP_LINES = 5; // Minimum lines of overlap for code chunks
export const MARKDOWN_OVERLAP_CHARS = 150; // Smaller overlap for markdown sections
export const ENABLE_OVERLAP = true; // Global overlap feature toggle

/**Search */
export const DEFAULT_SEARCH_MIN_SCORE = 0.7;
export const DEFAULT_MAX_SEARCH_RESULTS = 20;

/**File Watcher */
export const QDRANT_CODE_BLOCK_NAMESPACE = "f47ac10b-58cc-4372-a567-0e02b2c3d479";
export const MAX_FILE_SIZE_BYTES = 1 * 1024 * 1024; // 1MB

/**Directory Scanner */
export const MAX_LIST_FILES_LIMIT_CODE_INDEX = 50_000;
export const BATCH_SEGMENT_THRESHOLD = 60; // Number of code segments to batch for embeddings/upserts
export const MAX_BATCH_RETRIES = 3;
export const INITIAL_RETRY_DELAY_MS = 500;
export const PARSING_CONCURRENCY = 10;
export const MAX_PENDING_BATCHES = 20; // Maximum number of batches to accumulate before waiting
export const BATCH_PROCESSING_CONCURRENCY = 10;

/**OpenAI Embedder */
export const MAX_BATCH_TOKENS = 100000;
export const MAX_ITEM_TOKENS = 8191;

/**Gemini Embedder */
export const GEMINI_MAX_ITEM_TOKENS = 2048;

/**Supported file extensions for code scanning */
export const SUPPORTED_EXTENSIONS = [
  '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs',
  '.py', '.pyx', '.pyi',
  '.java', '.kt', '.scala',
  '.c', '.cpp', '.cc', '.cxx', '.h', '.hpp', '.hxx',
  '.cs', '.vb',
  '.go', '.rs', '.swift', '.rb', '.php',
  '.html', '.htm', '.xml', '.xhtml',
  '.css', '.scss', '.sass', '.less',
  '.json', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf',
  '.md', '.markdown', '.rst', '.txt',
  '.sh', '.bash', '.zsh', '.fish', '.ps1', '.bat', '.cmd',
  '.sql', '.sqlite', '.db',
  '.r', '.R', '.m', '.mm',
  '.pl', '.pm', '.lua', '.dart', '.elm', '.ex', '.exs',
  '.clj', '.cljs', '.cljc', '.lisp', '.scm', '.ss',
  '.hs', '.lhs', '.ml', '.mli', '.fs', '.fsi', '.fsx',
  '.dockerfile', '.Dockerfile',
  '.vue', '.svelte',
  '.tf', '.tfvars',
  '.makefile', '.Makefile',
  '.gradle', '.properties',
  '.lock', '.sum',
  // Fortran and scientific computing
  '.f', '.f90', '.f95', '.f03', '.f08', '.F', '.F90', '.F95', '.F03', '.F08',
  '.for', '.ftn', '.fpp',
  // MATLAB/Octave
  '.mat',
  // Additional text-based formats
  '.log', '.out', '.err', '.trace',
  '.patch', '.diff',
  '.env', '.envrc',
  '.gitignore', '.gitattributes',
  '.editorconfig',
  '.cmake', '.CMakeLists.txt',
  '.pro', '.pri', '.qrc',
  '.spec', '.rpmlintrc',
  // Documentation and config
  '.adoc', '.asciidoc', '.org', '.tex', '.bib',
  '.nfo', '.readme', '.changelog', '.license', '.authors',
  // Build and project files
  '.sln', '.vcproj', '.vcxproj', '.proj', '.targets', '.props',
  '.pbxproj', '.xcworkspace', '.xcodeproj'
];

/**
 * Check if a file appears to be a text file by examining its extension and content
 */
export function isTextFile(filePath: string): boolean {
  const ext = getFileExtension(filePath).toLowerCase();
  
  // Check if it's in our known supported extensions
  if (SUPPORTED_EXTENSIONS.includes(ext)) {
    return true;
  }
  
  // Files without extensions that are commonly text files
  const basename = filePath.split('/').pop()?.toLowerCase() || '';
  const textFilenames = [
    'readme', 'license', 'changelog', 'authors', 'contributors', 'copying',
    'install', 'news', 'todo', 'version', 'makefile', 'dockerfile',
    'gemfile', 'rakefile', 'vagrantfile', 'brewfile', 'podfile',
    'cmakelists.txt', 'configure', 'configure.ac', 'configure.in'
  ];
  
  if (textFilenames.includes(basename) || basename.startsWith('.')) {
    return true;
  }
  
  // Additional heuristics for unknown extensions
  // Allow files with no extension (common in Unix systems)
  if (!ext && !basename.includes('.')) {
    return true;
  }
  
  return false;
}

/**
 * Text file extensions - documentation, config, data files
 */
export const TEXT_FILE_EXTENSIONS = [
  '.md', '.markdown', '.rst', '.txt', '.log', '.out', '.err', '.trace',
  '.json', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf',
  '.xml', '.html', '.htm', '.xhtml',
  '.env', '.envrc', '.gitignore', '.gitattributes', '.editorconfig',
  '.patch', '.diff', '.nfo', '.readme', '.changelog', '.license', '.authors',
  '.adoc', '.asciidoc', '.org', '.tex', '.bib'
];

/**
 * Code file extensions - programming languages and source code
 */
export const CODE_FILE_EXTENSIONS = [
  '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs',
  '.py', '.pyx', '.pyi',
  '.java', '.kt', '.scala',
  '.c', '.cpp', '.cc', '.cxx', '.h', '.hpp', '.hxx',
  '.cs', '.vb',
  '.go', '.rs', '.swift', '.rb', '.php',
  '.css', '.scss', '.sass', '.less',
  '.sh', '.bash', '.zsh', '.fish', '.ps1', '.bat', '.cmd',
  '.sql', '.sqlite', '.db',
  '.r', '.R', '.m', '.mm',
  '.pl', '.pm', '.lua', '.dart', '.elm', '.ex', '.exs',
  '.clj', '.cljs', '.cljc', '.lisp', '.scm', '.ss',
  '.hs', '.lhs', '.ml', '.mli', '.fs', '.fsi', '.fsx',
  '.dockerfile', '.Dockerfile',
  '.vue', '.svelte',
  '.tf', '.tfvars',
  '.makefile', '.Makefile',
  '.gradle', '.properties',
  '.lock', '.sum',
  // Fortran and scientific computing
  '.f', '.f90', '.f95', '.f03', '.f08', '.F', '.F90', '.F95', '.F03', '.F08',
  '.for', '.ftn', '.fpp',
  // MATLAB/Octave
  '.mat',
  // Build and project files
  '.cmake', '.CMakeLists.txt',
  '.pro', '.pri', '.qrc',
  '.spec', '.rpmlintrc',
  '.sln', '.vcproj', '.vcxproj', '.proj', '.targets', '.props',
  '.pbxproj', '.xcworkspace', '.xcodeproj'
];

/**
 * Default search configuration for different file types
 */
export const SEARCH_DEFAULTS = {
  TEXT_FILES: {
    MIN_SCORE: 0.6,
    MAX_RESULTS: 10
  },
  CODE_FILES: {
    MIN_SCORE: 0.3,
    MAX_RESULTS: 10
  }
};

/**
 * Categorize a file as either text or code based on extension
 */
export function categorizeFileType(filePath: string): 'text' | 'code' | 'unknown' {
  const ext = getFileExtension(filePath).toLowerCase();
  
  if (TEXT_FILE_EXTENSIONS.includes(ext)) {
    return 'text';
  }
  
  if (CODE_FILE_EXTENSIONS.includes(ext)) {
    return 'code';
  }
  
  // Check for common text filenames without extensions
  const basename = filePath.split('/').pop()?.toLowerCase() || '';
  const textFilenames = [
    'readme', 'license', 'changelog', 'authors', 'contributors', 'copying',
    'install', 'news', 'todo', 'version', 'makefile', 'dockerfile',
    'gemfile', 'rakefile', 'vagrantfile', 'brewfile', 'podfile',
    'cmakelists.txt', 'configure', 'configure.ac', 'configure.in'
  ];
  
  if (textFilenames.includes(basename)) {
    return 'text';
  }
  
  return 'unknown';
}

/**
 * Get file extension from path
 */
function getFileExtension(filePath: string): string {
  const lastDot = filePath.lastIndexOf('.');
  const lastSlash = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
  
  // Make sure the dot is after the last slash (not part of directory name)
  if (lastDot > lastSlash && lastDot !== -1) {
    return filePath.substring(lastDot);
  }
  
  return '';
}
